/**
 * Created by Administrator on 2017/12/22.
 */
/**
 * 初始化部门详情对话框
 */
var CategoryInfoDlg = {
    categoryInfoData : {},
    validateFields: {
        categoryName: {
            validators: {
                notEmpty: {
                    message: '专辑名不能为空'
                }
            }
        },
        type: {
            validators: {
                notEmpty: {
                    message: '类型不能为空'
                }
            }
        },
        bannerList: {
            validators: {
                notEmpty: {
                    message: '所属栏目不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
CategoryInfoDlg.clearData = function() {
    this.categoryInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CategoryInfoDlg.set = function(key, val) {
    if(key == 'bannerList') {
        this.categoryInfoData[key] = (typeof value == "undefined") ? $("#" + key + " option:selected").val() : value;
    }
    else {
        this.categoryInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    }
    if (key=='intro'){
        var intro = CKEDITOR.instances.intro.getData();
        this.categoryInfoData[key]=intro;
    }
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
CategoryInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
CategoryInfoDlg.close = function() {
    parent.layer.close(window.parent.Category.layerIndex);
}

/**
 * 收集数据
 */
CategoryInfoDlg.collectData = function() {
    this.set('id').set('categoryName').set('iconPath').set('intro').set('type').set('bannerList').set('recRate');
}

/**
 * 验证数据是否为空
 */
CategoryInfoDlg.validate = function () {
    $('#categoryInfoForm').bootstrapValidator('validate');
    return $("#categoryInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加
 */
CategoryInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/category/video/add", function(data){
        Feng.success("添加成功!");
        window.parent.Category.table.refresh();
        CategoryInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    console.log(this.categoryInfoData);
    ajax.set(this.categoryInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
CategoryInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/category/video/update", function(data){
        Feng.success("修改成功!");
        window.parent.Category.table.refresh();
        CategoryInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.categoryInfoData);
    ajax.start();
};

/**
 * 批量修改
 */
CategoryInfoDlg.batchUpSubmit = function() {
    var ids = "";
    for(var i = 0;i < window.parent.Category.seItem.length;i++) {
        if(i < window.parent.Category.seItem.length - 1) {
            ids += window.parent.Category.seItem[i].id+",";
        } else {
            ids += window.parent.Category.seItem[i].id;
        }
    }
    var bannerId = $("#batchUpBannerList option:selected").val();

    var updateData = {};
    updateData['ids'] = ids;
    updateData['bannerId'] = bannerId;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/category/video/batchUpBanner", function(data){
        Feng.success("修改成功!");
        window.parent.Category.table.refresh();
        CategoryInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(updateData);
    ajax.start();
};

$(function () {
    Feng.initValidator("CategoryInfoForm", CategoryInfoDlg.validateFields);
    // 初始化头像上传
    var iconUp = new $WebIconUpload("iconPath");
    iconUp.setUploadBarId("progressBar");
    iconUp.init();


});
